package com.coder.algorithm.leetcode

/**
 * [118. 杨辉三角](https://leetcode-cn.com/problems/pascals-triangle/)
 *
 * @author yuhus
 * @date: 2022/3/29
 */
object l_0118 {
    fun generate(numRows: Int): List<List<Int>> {
        val result = ArrayList<List<Int>>()
        var preList = ArrayList<Int>()
        for (row in 1..numRows) {
            val rowList = ArrayList<Int>(row)
            for (i in 1..row) {
                // 第一个和最一个都为1
                if (i == 1 || i == row) {
                    rowList.add(1)
                } else {
                    val value = preList[i-2] + preList[i-1]
                    rowList.add(value)
                }
            }
            result.add(rowList)
            preList = rowList
        }
        return result
    }
}